/**
 * 
 */
package com.acc.dao.impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.acc.dao.OrderDao;
import com.acc.entity.Order;
import com.acc.entity.Standby;
import com.acc.utils.JDBCTools;

/**
 * @author 姜伟康 2017-9-12
 */
public class OrderDaoImpl extends JDBCTools implements OrderDao {

	
	// 获取连接
	Connection conn = null;
	// 创建PreparedStatement
	PreparedStatement ps = null;
	// 获取结果集
	ResultSet rs = null;
	
	Order order = null;
	
	int result = 0;
	
	// 用户查询所有订单
	@Override
	public List<Order> showAllOrder(String sql, Object... objects) {
		// TODO Auto-generated method stub

		List<Order> orders = new ArrayList<Order>();

		try {
			conn = super.getConnection();
			ps = conn.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {
				ps.setObject(i, object);
				i++;
			}
			rs = ps.executeQuery();
			// 获取结果
			while (rs.next()) {
				order = new Order();
				order.setOrderId(rs.getString("order_id"));
				order.setStartDate(rs.getDate("start_date"));
				order.setEndDate(rs.getDate("end_date"));
				order.setStartLocation(rs.getString("start_location"));
				order.setEndLocation(rs.getString("end_location"));
				order.setTrainNumber(rs.getInt("train_number"));
				order.setUsername(rs.getString("username"));
				order.setType(rs.getInt("type"));
				order.setStatus(rs.getString("status"));
				orders.add(order);
			}
			
			return orders;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.close(conn, ps, rs);

		}

   return null;
	}

	/**
	 * 根据订单号查询订单
	 * 
	 * @param orderId 订单号
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public Order selectOrder(String orderId) {
		// TODO Auto-generated method stub

		try {
			conn = super.getConnection();
			// 查询语句
			String sql = "select * from TI_ORDER where order_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, orderId);
			rs = ps.executeQuery();
			// 获取结果
			while (rs.next()) {
				order = new Order();
				order.setOrderId(rs.getString("order_id"));
				order.setStartDate(rs.getDate("start_date"));
				order.setEndDate(rs.getDate("end_date"));
				order.setStartLocation(rs.getString("start_location"));
				order.setEndLocation(rs.getString("end_location"));
				order.setTrainNumber(rs.getInt("train_number"));
				order.setUsername(rs.getString("username"));
				order.setType(rs.getInt("type"));
				order.setStatus(rs.getString("status"));
			}
			
			return order;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭连接
			super.close(conn, ps, rs);
		}
		return null;
	}

	/**
	 * 新增订单
	 * 
	 * @param username  用户名
	 * @param standby   车票
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public int addOrder(String orderId ,String username, Standby standby) {
		// TODO Auto-generated method stub


		try {
			conn = super.getConnection();
			// 创建插入语句
			String sql = "insert into TI_ORDER (order_id,start_date,end_date,start_location,end_location,train_number,username,type,status) values(?,sysdate,sysdate,?,?,?,?,?,'未支付')";
			ps = conn.prepareStatement(sql);
			ps.setString(1, orderId);
//			ps.setDate(2, (Date) standby.getStartDay());
//			ps.setDate(3, (Date) standby.getEndTime());
			ps.setString(2, standby.getStartLocation());
			ps.setString(3, standby.getEndLocation());
			ps.setInt(4, standby.getId());
			ps.setString(5,username);
			ps.setInt(6, standby.getType());
			// 执行修改
			result = ps.executeUpdate();
			return result ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.close(conn, ps, null);
			
		}

		
		return 0;
	}

	/**
	 * 取消订单
	 * 
	 * @param orderId  订单号
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public int deleteOrder(String orderId) {
		// TODO Auto-generated method stub


		try {
			conn = super.getConnection();
			// 创建删除语句
			String sql = "delete from TI_ORDER where order_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, orderId);
			// 执行修改
			result = ps.executeUpdate();
			return result ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.close(conn, ps, null);

		}
        return 0 ;
	}

	/**
	 * 改签
	 * 
	 * @param orderId     订单号
	 * @param standby     车票
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public int updateOrder(String orderId, Standby standby) {
		// TODO Auto-generated method stub


		try {
			conn = super.getConnection();
			// 创建插入语句
			String sql = "update TI_ORDER (order_id,start_date,end_date,start_location,end_location,train_number,username,type,status) values(?,?,?,?,?,?,?,1,1)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "session.username");
			ps.setDate(2, (Date) standby.getStartDay());
			ps.setDate(3, (Date) standby.getEndTime());
			ps.setString(4, standby.getStartLocation());
			ps.setString(5, standby.getEndLocation());
			ps.setInt(6, standby.getId());
			ps.setString(7, "session.username");
			// 执行修改
		    result = ps.executeUpdate();
		    return result ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			super.close(conn, ps, null);
		}
		return 0 ;
		
	}

	/**
	 * 修改票数
	 * 
	 * @param number        票数变动数量
	 * @param trainNumber   车次号
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public int updateStandyAmount(int number, Integer trainNumber) {
		// TODO Auto-generated method stub


		try {
			conn = super.getConnection();
			// 创建修改语句
			String sql = "update  TI_STANDBY set num=num+? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, number);
			ps.setInt(2, trainNumber);
			// 执行修改
			ps.executeUpdate();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			super.close(conn, ps, null);
		}
            return 0 ;
	}

	/**
	 * 修改订单状态
	 * 
	 * @param orderId    订单号
	 * @param status     订单状态
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public int updateOrderStatus(String orderId, String status) {
		// TODO Auto-generated method stub


		try {
			conn = super.getConnection();
			// 创建修改语句
			String sql = "update TI_ORDER set status=? where order_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, status);
			ps.setString(2, orderId);
			// 执行修改
			ps.executeUpdate();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.close(conn, ps, null);
		}
		return 0 ;

	}

	
	/**
	 * 根据车次查询车票
	 * 
	 * @param id    车次
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public Standby selectStandbyById(Integer id) {
		// TODO Auto-generated method stub

		Standby standby = null ;
		
		try {
			conn = super.getConnection();
			// 查询语句
			String sql = "select * from TI_STANDBY where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			// 获取结果
			while (rs.next()) {
				standby = new Standby();
				standby.setId(rs.getInt("id"));
				standby.setStartLocation(rs.getString("start_Location"));
				standby.setEndLocation(rs.getString("end_Location"));
				standby.setStartTime(rs.getDate("start_time"));
				standby.setStartDay(rs.getDate("start_day"));
				standby.setEndTime(rs.getDate("end_time"));
				standby.setPrice(rs.getInt("price"));
				standby.setNum(rs.getInt("num"));
				standby.setType(rs.getInt("type"));
			}
			
			return standby;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭连接
			super.close(conn, ps, rs);
		}
		return null;
	}

}
